﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Diagnostics;
using System.Collections;
using WatiNCF.Automater.Business.Adapters;

namespace WatiNCF.Automater.Controls
{
    public partial class ObjectEntryControl : BaseDataEntryControl
    {
        public object ObjectValue { get; set; }

        public ObjectEntryControl()
        {
            InitializeComponent();
        }

        public override void Initialise()
        {
            base.Initialise();
            try
            {
                if (!Adapter.IsProperty)
                {
                    ObjectValue = Activator.CreateInstance(Adapter.GetInfoType());
                }
                else
                {
                    ObjectValue = Adapter.GetValue();
                }
                ShowProperties(ObjectValue);
            }
            catch
            {
                Debug.WriteLine(string.Format("Could not get value for {0}", Adapter.GetName()));
            }
        }

        private void ShowProperties(object target)
        {
            PropertyInfo[] properties = target.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            this.PropertiesPanel.Controls.Clear();
            int count = properties.Length;
            foreach (var property in properties.Reverse())
            {
                if (target.GetType().GetInterface("IList") != null ||
                    target.GetType().GetInterface("ICollection") != null ||
                    target.GetType().IsArray)
                {
                    // Do nothing
                }
                else
                {
                    BaseDataEntryControl control = DataEntryControlFactory.GetControl(this.ObjectValue, property);
                    if (control != null)
                    {
                        control.Dock = DockStyle.Top;
                        control.TabIndex = count;
                        this.PropertiesPanel.Controls.Add(control);
                        IncreaseControlSize(control.Height);

                        if (count == 1)
                        {
                            control.SetFocus();
                        }
                    }
                    count--;
                }
            }
        }

        private void IncreaseControlSize(int height)
        {
            this.Height += height;
        }

        public override void SetName(string name)
        {
            this.ObjectGroupBox.Text = name;
        }

        public override object GetValue()
        {
            foreach (var control in this.PropertiesPanel.Controls.OfType<BaseDataEntryControl>())
            {
                control.GetValue();
            }
            return this.ObjectValue;
        }

        public override void SetFocus()
        {
            this.ObjectGroupBox.Focus();
        }
    }
}
